<template>
  <div>
    <div style="margin: 20px 0"></div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="105" :isSubmitBtn="false" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)" :disabled="isreadonly">保 存</multiuse-button>
      <multiuse-button @click="closeDrawer(1)">关 闭</multiuse-button>
    </div>
  </div>
</template>

<script>
import config from '@/assets/js/config';
export default {
  props: ['rowData'],
  data() {
    return {
      formList: this.createFromList(),
      fileList: [],
      rowIndex: undefined,
      isreadonly: false
    };
  },
  mounted() {
    console.log('初始化' + JSON.stringify(this.rowData));
    if (this.rowData) {
      let url = this.rowData.paymentVoucherUrl;
      if (url) {
        let name = this.rowData.paymentVoucherName;
        let initData = {
          name: name,
          url: url
        };
        this.fileList.push(initData);
      }
      this.rowIndex = this.rowData.index;
      if (this.rowData.readonly) {
        this.isreadonly = true;
        this.formList.find(item => item.fieldName === 'fileList').disabled = true;
      }
      this.formList.find(item => item.fieldName === 'fileList').initialValue = this.fileList;
    }
  },
  created() {},
  methods: {
    createFromList() {
      return [
        {
          type: 'UPLOAD_FILE',
          label: '附件上传',
          fieldName: 'fileList',
          initialValue: [],
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            limit: 1,
            max: 1024 * 100
          },
          selfCols: 3
        }
      ];
    },
    async closeDrawer(type) {
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();
      if (err) return;
      //   console.log('closeDrawer=>', JSON.stringify(data));
      //   console.log('return=>', { ...data.fileList[0], index: this.rowIndex });
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$notify.success({ title: '提示信息', message: '文件保存成功' });
        this.$emit('close', { ...data.fileList[0], index: this.rowIndex });
      }
    }
  }
};
</script>

<style></style>
